package com.tool_stdy.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * Created by yan_li on 2016/1/24.
 */
public class SemaphoreTest implements Runnable {
    final Semaphore sam = new Semaphore(5);
    @Override
    public void run() {
        try {
            sam.acquire();
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName()+"=--- done");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            sam.release();
        }
    }

    public static void main(String[] args) {
        SemaphoreTest test = new SemaphoreTest();
        ExecutorService esec = Executors.newFixedThreadPool(20);
        for(int i=0;i<20;i++){
            esec.submit(test);
        }
        esec.shutdown();
    }
}
